<template>
  <div class="admin-rz">
    <el-form class="add-list-data" :model="form" ref="form">
      <h3>发布新日志</h3>
      <el-form-item
        prop="label"
        :rules="[{ required: true, message: '标题不能为空' },{ min: 2, max: 24,message: '长度在2到24个字符之间' }]"
      >
        <el-input type="text" v-model="form.label" autocomplete="off" placeholder="请输入日志标题"></el-input>
      </el-form-item>
      <el-form-item
        class="body"
        prop="body"
        :rules="[{ required: true, message: '内容不能为空' },{ min: 2, max: 10000,message: '长度在2到10000个字符之间' }]"
      >
        <el-input
          type="textarea"
          :autosize="{minRows: 6}"
          v-model="form.body"
          placeholder="请输入日志内容"
        ></el-input>
      </el-form-item>
      <div class="select">
        <el-upload
          class="upload-demo"
          action
          list-type="picture"
          :http-request="addAttachment"
          :limit="limitNum"
          :before-upload="beforeAvatarUpload"
          ref="upload"
        >
          <el-button slot="trigger" size="small" type="primary">选取图片</el-button>
        </el-upload>
        <div class="hot">
          <el-radio-group v-model="form.hot">
            <el-checkbox :label="!!true">热门</el-checkbox>
          </el-radio-group>
        </div>
        <div class="btn">
          <el-button @click="submitForm('form')">上传</el-button>
          <el-button @click="resetRuleForm('form')">重置</el-button>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
import { mapState, mapActions } from "Vuex";
import { userUrl } from "@/public/uri.js";
export default {
  name: "Admin-rz",
  data() {
    return {
      form: {
        label: "",
        body: "",
        hot: false,
        age: ""
      },
      fileData: {},
      limitNum: 1,
      numberValidateForm: { age: "" }
    };
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/png" || file.type === "image/jpeg";
      const isLt1M = file.size / 1024 / 1024 < 1;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG/PNG 格式!");
      }
      if (!isLt1M) {
        this.$message.error("上传头像图片大小不能超过 1MB!");
      }
      return isJPG && isLt1M;
    },
    resetRuleForm(form) {
      this.$refs[form].resetFields();
    },
    addAttachment(file) {
      this.fileData = file.file;
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (!this.fileData.size) {
            this.$alert("请至少选择一张图片");
            return;
          }
          let fileFormData = new FormData();
          fileFormData.append("image", this.fileData);
          fileFormData.append("label", this.form.label);
          fileFormData.append("body", this.form.body);
          fileFormData.append("hot", this.form.hot);
          fileFormData.append("userName", this.userName);
          if (!fileFormData.get("image").name) {
            return;
          }
          this.$axios
            .post(`${userUrl}/userdata/setrzdata`, fileFormData)
            .then(res => {
              if (res.data.ok) {
                this.$alert(res.data.data);
                this.resetRuleForm("form");
                this.fileData = {}
                this.$refs.upload.clearFiles();
                this.getRzData(this.userName);
                this.getUserInfoData(this.userName);
              }
            })
            .catch(err => {
              if (err) {
                console.log("err：" + err);
              }
            });
        } else {
          return false;
        }
      });
    },
    ...mapActions("user", ["getUserInfoData", "getRzData"])
  },
  computed: {
    ...mapState("user", ["userName"])
  }
};
</script>

<style lang="sass" scoped> 
@import '@/assets/default.sass'
.admin-rz
  width: 100%
  .add-list-data
    width: 90%
    margin: 0 auto
    padding: 5% 0
    background-color: #fff
    h3
      line-height: 60px
    .title
      width: 100%
      height: 40px
      line-height: 40px
    .body
      margin-top: 10px
    .select
      margin-top: 40px
      @include flex(space-between,center)

</style>